'''
Created on Mar 9, 2011

@author: phi
'''

class svg(object):
    '''
    classdocs
    '''


    def __init__(self):
        with open('base.svg','r') as f:
            self.svg = '\n'.join(f.readlines()) 
        self.innersvg = ''
#    def add_js(self,content):
#        self.jsref += '''<script type="text/javascript">%s</script>''' % (content)

    def add(self,element):
        self.innersvg+=str(element)+'\n'
    def get_svg(self):
        return self.svg % (self.innersvg)

class svg_item:
    
    def __init__(self,fields = dict(),style=dict(),svg_type='path'):
        self.elements = dict()
        for k,v in fields.items():
            self.elements[k] = v
        stylei = ''
        for k,v in style.items():
            stylei += '%s:%s; ' % (k,v)
        self.elements['style'] = stylei
        
        self.svg_type =svg_type
        
        
            
    def print_elements(self):
        elements = ""
        for k,v in self.elements.items():
            elements += '%s="%s" ' % (k,v)
        return elements
    
    def __str__(self):
        return '''<%s %s/>''' % (self.svg_type,self.print_elements())
        
        
class circle_wedge(svg_item):
    def __init__(self,x1,y1,x2,y2,rx,ry,x_rotate=0,large_arch=0,sweep_flag=0,fields=dict(),style=dict()):
        fields['d']='''M %(x1)s,%(y1)s A%(rx)s,%(ry)s %(x_rotate)s
            %(large_arch)s,%(sweep_flag)s %(x2)s,%(y2)s'''% locals()
        svg_item.__init__(self, fields, style)
#        
#    svg = '''\n<path  id="%(id)s" d="M %(x1)s,%(y1)s A%(rx)s,%(ry)s %(x_rotate)s
#    %(large_arch)s,%(sweep_flag)s %(x2)s,%(y2)s "'''% locals()
#    return '%s %s %s />' % (svg,style,events )
                    
class line(svg_item):
    def __init__(self,x1,y1,x2,y2,fields=dict(),style=dict()):
        fields['d']=d="M%s,%s %s %s, %s %s" % (x1,y1,x1,y1,x2,y2)
        svg_item.__init__(self, fields, style)


class rectangle(svg_item):
    def __init__(self,x,y,width,height,fields=dict(),style=dict()):
        fields['x']=x
        fields['y']=y
        fields['width']=width
        fields['height']=height
        svg_item.__init__(self, fields, style,svg_type='rect')
        
class square(rectangle):
    def __init__(self,x,y,width,fields=dict(),style=dict()):
        rectangle.__init__(self, x, y, width, width, fields, style)
#    svg = '''\n<path id="%s" d="M%s,%s %s %s, %s %s"
#        style="stroke:%s;stroke-width:%s; fill:none;" onclick="%s" onmouseover="%s" onmouseout="%s"/>\n''' 
#    return svg % (id,x1,y1,x1,y1,x2,y2,color,width,onclick,onmouseover,onmouseout)
class text(svg_item):
    def __init__(self,x,y,text,fields=dict(),style=dict(),text_anchor='start'):
        self.text = text
        fields['x']= '''%(x)s''' % locals()
        fields['y']=  '''%(y)s''' % locals()
                
        if not style.has_key('text-anchor'):
            style['text-anchor']=text_anchor
        svg_item.__init__(self, fields, style)
    
    def __str__(self):
        return '''<text %s>%s</text>''' % (self.print_elements(),self.text)
    
#    svg = '''\n<text id="%(id)s" onclick="%(onclick)s" onmouseover="%(onmouseover)s" onmouseout="%(onmouseout)s"
#                x="%(x)s"  y="%(y)s"
#                style="font-family: %(font)s;
#                text-anchor: %(text_anchor)s;
#                font-size  : %(font_size)spx;
#                fill       : %(color)s;">%(text)s</text>\n''' % locals()
#    return svg 

#class rectangle(svg_item):
#    def __init__(self,x,y,w,h,text):
#    svg = '''\n<text id="%(id)s" onclick="%(onclick)s" onmouseover="%(onmouseover)s" onmouseout="%(onmouseout)s"
#                x="%(x)s"  y="%(y)s"
#                style="font-family: %(font)s;
#                text-anchor: %(text_anchor)s;
#                font-size  : %(font_size)spx;
#                fill       : %(color)s;">%(text)s</text>\n''' % locals()
#    return svg 

class linkify:
    def __init__(self,svg,url):
        self.svg = svg
        self.url = url
    def __str__(self):
        return '''<a xlink:href="%s" xlink:show="new">%s</a>''' % (self.url,self.svg)
#def linkify(svg,url):
#    svg = '''<a xlink:href="%(url)s" xlink:show="new">%(svg)s</a>''' % locals()
#    return svg

class curve(svg_item):
    def __init__(self,x1,y1,x2,y2,x3,y3,fields=dict(),style=dict()):
        fields['d']=d="M%s,%s C %s %s, %s %s, %s %s" % (x1,y1,x1,y1,x2,y2,x3,y3)
        svg_item.__init__(self, fields, style)
        
#    svg = '''\n<path id="%s" d="M%s,%s C %s %s, %s %s, %s %s"
#          style="stroke:%s;stroke-width:%s; fill:none;" onclick="%s" onmouseover="%s" onmouseout="%s"/>\n''' 
#    return svg % (id,x1,y1,x1,y1,x2,y2,x3,y3,color,width,onclick,onmouseover,onmouseout)



#def curve_styled(self,x1,y1,x2,y2,x3,y3,style):
#    svg = '''\n<path d="M%s,%s C %s %s, %s %s, %s %s"
#          style="%s"/>\n''' 
#    return svg % (x1,y1,x1,y1,x2,y2,x3,y3,style)
        
         

    
